package com.outsourcing.bottle.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.outsourcing.bottle.domain.OtherConfigEntry;

/**
 * 
 * @author 06peng
 *
 */
public class OtherConfigTable {

	private final String TABLE_BOTTLE_OTHER_CONFIG = "bottle_other_config";
	private DBManagerImpl db = null;
	public OtherConfigTable() {
		if (db == null) {
			db = DBManager.get();
		}
		if (!db.isTableExits(db.getConnection(), TABLE_BOTTLE_OTHER_CONFIG)) {
			createBottleNetType();
		}
	}
	
	public OtherConfigTable(Context context) {
		if (db == null) {
			db = DBManager.get(context);
		}
		if (!db.isTableExits(db.getConnection(), TABLE_BOTTLE_OTHER_CONFIG)) {
			createBottleNetType();
		}
	}
	
	private void createBottleNetType() {
		String createSql = "create table if not exists "+TABLE_BOTTLE_OTHER_CONFIG+" (id integer primary key autoincrement, feedback_uid integer, qq_enable integer, weibo_enable integer," +
				"tencent_enable integer, renren_enable integer, douban_enable integer, twitter_enable integer, facebook_enable integer, register_enable integer, last_android_version varchar)";
		db.creatTable(db.getConnection(), createSql, TABLE_BOTTLE_OTHER_CONFIG);
	}
	
	public void saveBottleOtherConfig(OtherConfigEntry entry) {
		ContentValues values = new ContentValues();
		values.put("feedback_uid", entry.getFeedback_uid());
		values.put("qq_enable", entry.getQq_enable());
		values.put("weibo_enable", entry.getWeibo_enable());
		values.put("tencent_enable", entry.getTencent_enable());
		values.put("renren_enable", entry.getRenren_enable());
		values.put("douban_enable", entry.getDouban_enable());
		values.put("twitter_enable", entry.getTwitter_enable());
		values.put("facebook_enable", entry.getFacebook_enable());
		values.put("register_enable", entry.getRegister_enable());
		values.put("last_android_version", entry.getLast_android_version());
		db.save(db.getConnection(), TABLE_BOTTLE_OTHER_CONFIG, values);
	}
	
	public OtherConfigEntry getBottleOtherConfig() {
		Cursor cursor = null;
		OtherConfigEntry entry = null;
		try {
			cursor = db.find(db.getConnection(), "select * from " + TABLE_BOTTLE_OTHER_CONFIG, null);
			if (cursor != null) {
				while (cursor.moveToNext()) {
					entry = new OtherConfigEntry();
					entry.setFeedback_uid(cursor.getInt(cursor.getColumnIndex("feedback_uid")));
					entry.setDouban_enable(cursor.getInt(cursor.getColumnIndex("douban_enable")));
					entry.setFacebook_enable(cursor.getInt(cursor.getColumnIndex("facebook_enable")));
					entry.setQq_enable(cursor.getInt(cursor.getColumnIndex("qq_enable")));
					entry.setRenren_enable(cursor.getInt(cursor.getColumnIndex("renren_enable")));
					entry.setTencent_enable(cursor.getInt(cursor.getColumnIndex("tencent_enable")));
					entry.setTwitter_enable(cursor.getInt(cursor.getColumnIndex("twitter_enable")));
					entry.setWeibo_enable(cursor.getInt(cursor.getColumnIndex("weibo_enable")));
					entry.setRegister_enable(cursor.getInt(cursor.getColumnIndex("register_enable")));
					entry.setLast_android_version(cursor.getString(cursor.getColumnIndex("last_android_version")));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
		return entry;
	}
	
	public void clearTable() {
		db.delete(db.getConnection(), TABLE_BOTTLE_OTHER_CONFIG, null, null);
	}
}
